<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Models\Api\V1\Article;
use App\Models\Common;
use Illuminate\Http\Request;

class ArticleContorller extends Controller
{

    public function __construct()
    {
        $this->middleware('apitoken')->except(['index','show']);
    }

    public function index(Request $request)
    {
        $res = Article::orderby('id','desc')
            ->where(function ($query) use ($request){
                $keyword = $request->input('keyword');
                if(!empty($keyword)){
                    $query->where('title','like','%'.$keyword.'%');
                }
            })->Paginate($request->query('perPage',10))->toArray();
        Common::resJson(['code'=>800,'data'=>$res,'msg'=>'获取成功']);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $article = new Article;
        $article->title=$request->input('title');
        $article->content=$request->input('content');
        $article->user_id=$request->api_user->id;
        if($article->save()){
            Common::resJson(['code'=>800,'data'=>$article->toArray(),'msg'=>'发布成功']);
        }else{
            Common::resJson(['code'=>801,'data'=>'','msg'=>'发布失败']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $res = Article::find($id)->toArray();
        Common::resJson(['code'=>800,'data'=>$res,'msg'=>'获取成功']);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $article = Article::where('id', '=', $id)
                          ->where('user_id', '=', $request->api_user->id)->first();
        if($article){
            $article->title=$request->input('title');
            $article->content=$request->input('content');
            $article->user_id=$request->api_user->id;
            if($article->save()){
                Common::resJson(['code'=>800,'data'=>$article->toArray(),'msg'=>'编辑成功']);
            }
        }
        Common::resJson(['code'=>801,'data'=>'','msg'=>'编辑失败']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id,Request $request)
    {
        $article = Article::where('id', '=', $id)
            ->where('user_id', '=', $request->api_user->id)->first();
        if($article){
            if($article->delete()){
                Common::resJson(['code'=>800,'data'=>'','msg'=>'删除成功']);
            }
        }
        Common::resJson(['code'=>802,'data'=>'','msg'=>'删除失败']);
    }

    public function myindex(Request $request)
    {
        $res = Article::orderby('id','desc')
            ->where(function ($query) use ($request){
                $keyword = $request->input('keyword');
                if(!empty($keyword)){
                    $query->where('title','like','%'.$keyword.'%');
                }
            })->where('user_id', '=', $request->api_user->id)
            ->Paginate($request->query('perPage',10))->toArray();
        Common::resJson(['code'=>800,'data'=>$res,'msg'=>'获取成功']);
    }
}
